package com.rocsea.robot.service;

import com.rocsea.robot.dao.DirectoryDAO;
import com.rocsea.robot.exception.BusinessException;
import com.rocsea.robot.model.bo.DirectoryPoint;
import com.rocsea.robot.model.entity.DirectoryDO;
import com.rocsea.robot.model.enums.KettleBuildModuleEnum;
import com.rocsea.robot.model.request.KettleExportRequest;
import com.rocsea.robot.utils.KettleBuildUtils;
import com.rocsea.robot.utils.MysqlBuilderUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author RocSea
 * @Date 2022/12/7
 */
@Service
public class DirectoryService {

    private final DirectoryDAO directoryDAO;

    public DirectoryService(DirectoryDAO directoryDAO) {
        this.directoryDAO = directoryDAO;
    }


    public DirectoryPoint getPoints() {
        DirectoryPoint directoryPoint = new DirectoryPoint();
        List<DirectoryDO> directory = new ArrayList<>();
        int moduleType = KettleBuildUtils.getModuleType();
        String sourceSchemaName = KettleBuildUtils.getSourceSchemaName();
        String sourceTableName = KettleBuildUtils.getSourceTableName();
        Optional<DirectoryDO> parent = directoryDAO.getOne(moduleType, sourceSchemaName);
        Optional<Long> maxId = directoryDAO.getMaxId();
        Long nextId = maxId.orElse(0L);
        if (parent.isPresent()) {
            Optional<DirectoryDO> child = directoryDAO.getOne(parent.get().getIdDirectory().intValue(), sourceTableName);
            if (child.isPresent()) {
                nextId = child.get().getIdDirectory();
            } else {
                DirectoryDO childPoint = new DirectoryDO();
                childPoint.setIdDirectory(++nextId);
                childPoint.setIdDirectoryParent(parent.get().getIdDirectory().intValue());
                childPoint.setDirectoryName(sourceTableName);
                directory.add(childPoint);
            }
        } else {
            DirectoryDO parentPoint = new DirectoryDO();
            parentPoint.setIdDirectory(++nextId);
            parentPoint.setIdDirectoryParent(moduleType);
            parentPoint.setDirectoryName(sourceSchemaName);
            directory.add(parentPoint);

            long parentId = nextId;
            DirectoryDO childPoint = new DirectoryDO();
            childPoint.setIdDirectory(++nextId);
            childPoint.setIdDirectoryParent((int) parentId);
            childPoint.setDirectoryName(sourceTableName);
            directory.add(childPoint);
        }
        KettleBuildUtils.setIdDirectory(nextId);
        directoryPoint.setDirectory(directory);
        return directoryPoint;
    }

    public Boolean check(Integer moduleType, String schemaName) {
        KettleBuildModuleEnum.geText(moduleType);
        Optional<DirectoryDO> parent = directoryDAO.getOne(moduleType, schemaName);
        return parent.isPresent();
    }

    public Long export(KettleExportRequest request, StringBuilder sql) {
        KettleBuildModuleEnum.geText(request.getModuleType());
        Optional<DirectoryDO> parent = directoryDAO.getOne(request.getModuleType(), request.getSourceSchemaName());
        if (!parent.isPresent()) {
            throw new BusinessException("父目录不存在！");
        }

        List<DirectoryDO> childList = directoryDAO.getList(parent.get().getIdDirectory().intValue());
        if (childList.size() == 1){
            sql.append(MysqlBuilderUtils.buildDirectory(parent.get()));
        }
        DirectoryDO directoryDO =
                childList.stream().filter(x -> x.getDirectoryName().equals(request.getSourceTableName())).findFirst().get();

        sql.append(MysqlBuilderUtils.buildDirectory(directoryDO));
        return directoryDO.getIdDirectory();
    }
}
